<?php

namespace app\admin\controller;

use app\admin\model\Article;
use app\admin\model\Category;
use app\admin\model\Comment;
use app\admin\model\Fake;
use app\admin\model\Files;
use app\admin\model\Good;
use app\admin\model\OperationLog;
use app\admin\model\Order;
use app\admin\model\User;
use think\facade\Db;

class Index extends Base
{
    public function index()
    {
        $user = Db::name('user')->where(['id' => $this->uid])->find();
        return view('index', ['user' => $user]);
    }
    public function main()
    {
        $startUp = include app()->getRootPath() . '/app/api/config/startUp.php';
        $newCommentList = Comment::with(['article' => function ($query) {
            $query->field('id,title');
        }, 'userProfile' => function ($query) {
            $query->field('id , qq , nickname');
        }])->order('id','desc')->select();
        $data = [
            'artTotal' => Article::count(),
            'artNoExamine' =>  Article::where(['status' => 0])->count(),
            'userTotal' => User::count(),
            'userNoExamine' => User::where(['status' => 0])->count(),
            'cateTotal' => Category::count(),
            'cateShow' => Category::where(['status' => 1])->where('pid', '<>', 0)->count(),
            'commentList' => $newCommentList,
            'commentToday' => Comment::whereDay('create_time')->count(),
            'commentTotal' => Comment::count(),
            'goodTotal' => Good::count(),
            'avatorTotal' => Db::name('avator')->count(),
            'avatorNoExamine' => Db::name('avator')->where(['status' => 0])->count(),
            'fake' => Fake::count(),
            'order' => ['total' =>  Order::count(), 'totaySuccess' => Order::whereDay('create_time')->where(['status' => 1])->count(), 'todayMoney' => Order::whereDay('create_time')->where(['status' => 1])->count('money')],
            // 启动次数
            'startUp' => $startUp,
            // 日志
            'log' => ['total' => OperationLog::count(), 'todayError' => OperationLog::where(['status' => 0])->count()],
            // 上架商品
            'goodUp' => Good::where('status', 1)->count(),
            'files' => ['total' => Files::count()]
        ];
        return view('main', $data);
    }
}
